<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once ('api/IDataHolder.php');

/**
 *
 * Default implementation of IDataHolder
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class SimpleDataHolder implements IDataHolder
{

  protected $data;

  protected $modified;

  public function __construct($data = null)
  {
    $this->data = new Dictionary();
    $this->modified = new Dictionary();
    $this->reset($data);
  }

  public function __get($name)
  {
    return $this->get($name);
  }

  public function __set($name, $value)
  {
    return $this->set($name, $value);
  }

  public function commit()
  {
    $this->data->copyAll($this->modified);
    $this->modified->reset();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#copyAll($data)
   */
  public function copyAll($data)
  {
    if (is_a($data, 'IDictionary')) {
      $data = $data->getAll();
    }

    foreach ($data as $name => $value) {
      $this->set($name, $value);
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  public function get($name)
  {
    $args = func_get_args();

    $func = array($this, '_get');
    if (method_exists($this, 'get_' . $name)) {
      $func = array($this, 'get_' . $name);
      unset($args[0]);
    }

    return call_user_func_array($func, $args);
  }

  public function getModified()
  {
    return $this->modified->getAll();
  }

  public function del($name)
  {
    $this->data->del($name);
    $this->modified->del($name);
  }

  public function getAll()
  {
    return array_merge($this->data->getAll(), $this->modified->getAll());
  }

  public function getNames()
  {
    return array_merge($this->data->getNames(), $this->modified->getNames());
  }

  public function has($name)
  {
    return $this->modified->has($name) || $this->data->has($name);
  }

  public function reset($data = null)
  {
    $this->data->reset();
    $this->modified->reset($data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  public function set($name, $value)
  {
    $args = func_get_args();

    $func = array($this, '_set');
    if (method_exists($this, 'set_' . $name)) {
      $func = array($this, 'set_' . $name);
      unset($args[0]);
    }

    return call_user_func_array($func, $args);
  }

  protected function _get($name)
  {
    if ($this->modified->has($name)) {
      return $this->modified->get($name);
    }

    return $this->data->get($name);
  }

  protected function _set($name, $value)
  {
    if ($this->data->get($name) === $value) {
      $this->modified->del($name);
    }
    else {
      $this->modified->set($name, $value);
    }
  }

}

?>